﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Text;
using Liger.Web.Common;
using Liger.Web.Common.Extensions;
using Liger.Web.Javascript;
using System.Web.Script.Serialization;

namespace Liger.Web.UI
{

    public abstract class UIComponentBase  : IHideObjectMembers
    {
        protected StringBuilder Writer;
        protected IJavascriptObjectWriter JSWriter;

        public UIComponentBase()
            :this(null)
        {
            
        }

        public UIComponentBase(StringBuilder writer)
        {
            this.Writer = writer;
            this.JSWriter = new JavascriptObjectWriter(this.Writer);
            this.JSWriter.PropertyVariableCase = true;
            this.Events = new Dictionary<string, Func<string>>();
            this.LoadInDocumentReady = false;
            this.OutputHtml = true;
            this.OutputScript = true;
        }

        protected string GetObjectJSON(object obj)
        {
            return new JavaScriptSerializer().Serialize(obj);
        }


        /// <summary>
        /// 插件名称
        /// </summary>
        public virtual string PluginName
        {
            get;
            set;
        }

        /// <summary>
        /// 在页面加载完成执行脚本
        /// </summary>
        public bool LoadInDocumentReady { get; set; }

        /// <summary>
        /// 事件
        /// </summary>
        public virtual Dictionary<string,Func<string>> Events
        {
            get;
            set;
        }
      

        /// <summary>
        /// 组件名称
        /// </summary>
        public virtual string ComponentName
        {
            get;
            set;
        }

        /// <summary>
        /// UI Component  ID
        /// </summary>
        public virtual string ID
        {
            get;
            set;
        }

        /// <summary>
        /// UI Component TagName
        /// </summary>
        public virtual string TagName
        {
            get;
            set;
        }

        /// <summary>
        /// 输出html节点
        /// </summary>
        public virtual bool OutputHtml
        {
            get;
            set;
        }
        /// <summary>
        /// 输出脚本
        /// </summary>
        public virtual bool OutputScript
        {
            get;
            set;
        }

        protected virtual string NewID()
        {
            string prev = string.IsNullOrEmpty(ComponentName) ? "UI" : ComponentName;
            return prev + (UIComponent.RenderComponentIDStart + UIComponent.GetRenderComponentNumber()).ToString();
        }


        /// <summary>
        /// Write HTML
        /// </summary>
        /// <returns></returns>
        public virtual void WriteHTML()
        { 
        }


        /// <summary>
        /// Write Javascript Content
        /// </summary>
        /// <returns></returns>
        public virtual void WriteScript()
        {  
        }  
        
        public virtual void Render()
        {
            UIComponent.TagRenderComponent();
            if (!ID.HasValue()) ID = NewID();
            if (OutputHtml)
            {
                this.WriteHTML();
            }
            Writer.AppendLine();
            Writer.AppendLine("<script type='text/javascript'>");
            if (OutputScript)
            {
                this.WriteScript();
            }
            Writer.AppendLine("</script>");
        } 

        public override string ToString()
        {
            return Writer.ToString();
        }
    }
}
